package cn.edu.ntu.yyl.service.imlp;

import cn.edu.ntu.yyl.dao.ArticleCategoryDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    ArticleCategoryDao Dao;
    //添加文章分类(标签)
    public int AddNewArticleCategory(ArticleCategory articleCategory){
        //初始化

        try {
            //getMapper
            //执行SQL
            articleCategory.setId(getmaxid());
            //获取当前时间
            Date date=new Date(System.currentTimeMillis());
            articleCategory.setCreated(date);
            articleCategory.setType("classify");
            Dao.AddNewCategory(articleCategory);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //删除文章分类(标签)
    public int DeleteArticleCategory(ArticleCategory articleCategory){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.DeleteCategory(articleCategory);

        } catch (Exception e) {
            return -1;

        }
        return 1;
    }
    //修改文章分类(标签)
    public int UpdateArticleCategory(ArticleCategory articleCategory){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateCategory(articleCategory);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //查询所有分类
    public List<ArticleCategory> SelectAllArticleCategory(){
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCategory();
        } catch (Exception e) {
            return null;
        }
    }
    //查询父分类
    public ArticleCategory SelectCategoryFather(ArticleCategory articleCategory) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCategoryFather(articleCategory);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查询子分类
    public List<ArticleCategory> SelectCategoryChildren(ArticleCategory articleCategory) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCategoryChildren(articleCategory);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查询文章相关的分类
    public ArticleCategory SelectCategoryByArticle(Article article) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCategoryByArticle(article);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //按ID查询分类
    public ArticleCategory SelectCategoryById(Integer id) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCategoryById(id);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //添加标签
    public int AddNewTag(ArticleCategory articleCategory) {
        //初始化

        try {
            //getMapper

            //执行SQL
            articleCategory.setId(getmaxid());
            //获取当前时间
            Date date=new Date(System.currentTimeMillis());
            articleCategory.setCreated(date);
            articleCategory.setType("tag");
            Dao.AddNewTag(articleCategory);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //删除标签
    public int DeleteTag(ArticleCategory articleCategory) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.DeleteTag(articleCategory);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //修改标签
    public int UpdateTag(ArticleCategory articleCategory) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateTag(articleCategory);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //查询标签
    public List<ArticleCategory> SelectTag() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectTag();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查询文章的标签
    public List<ArticleCategory> SelectTagByArticle(Article article) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectTagByArticle(article);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //按ID查询标签
    public ArticleCategory SelectTagById(Integer id) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectTagById(id);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //分页查询分类
    public List<ArticleCategory> SelectCategoryBylimit(int index, int pageSize){
        return null;//Dao.SelectCategoryBylimit((index-1)*pageSize,index*pageSize-1);
    }
    //分页查询标签
    public List<ArticleCategory> SelectTagBylimit(int index, int pageSize){
    return null;//Dao.SelectTagBylimit((index-1)*pageSize,index*pageSize-1);
    }

    //获取最大ID
    private Integer getmaxid() {
        //初始化
        List<ArticleCategory> articleCategorys = null;
        Integer id = -1;
        //取得SqlSession对象(openSession)

        try {
            //getMapper

            //获取列表
            articleCategorys = Dao.SelectAll();
        } catch (Exception e) {
            System.err.println(e.toString());

        }
        //获取最大ID然后+1便是需要写入的ID
        if (articleCategorys != null) {
            for (ArticleCategory articleCategory : articleCategorys) {
                if (id < articleCategory.getId()) {
                    id = articleCategory.getId();
                }
            }
        } else return -1;
        id++;
        return id;
    }


}
